package files.mover;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.io.FilenameUtils;

import static files.mover.commons.Colors.ANSI_BLUE;
import static files.mover.commons.Colors.ANSI_PURPLE;
import static files.mover.commons.Colors.ANSI_RESET;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class FileTreeServise {

    private final Set<String> folderNames;

    public void listFilesForFolder(final File folder) {
        int filesCounter = 0;
        Set<String> folderNames = new HashSet<>();
        for (final File fileEntry : folder.listFiles()) {
            String fileName = fileEntry.getName();
            String extension = FilenameUtils.getExtension(fileName);
            if (fileEntry.isDirectory()) {
                listFilesForFolder(fileEntry);
            } else {
                if (!folderNames.contains(extension)) {
                    try {
                        String folderNameFinal = Paths
                                .get("D:\\testdata\\" + extension).toString();
                        System.out.println(folderNameFinal);
                        Files.createDirectory(
                                Paths.get("D:\\testdata\\" + extension));
                        folderNames.add(extension);
                    } catch (final IOException e) {
                        System.out.println("Невозможно создать папку: " + e);
                    }
                }
                folderNames.add(fileName);
                if (fileName.endsWith("ORF")) {
                    System.out.println(ANSI_PURPLE.value + fileName + ANSI_RESET.value);
                } else if (fileName.endsWith("NEF")) {
                    System.out.println(ANSI_BLUE.value + fileName + ANSI_RESET.value);
                } else {
                    System.out.println(fileEntry.getName());
                }
                filesCounter++;
            }
        }
        System.out.println("Всего файлов: " + filesCounter);
        for (String fld : folderNames) {
            System.out.println("Папка с именем: " + fld + " СУЩЕСТВУЕТ");
        }
    }
}
